<template>
  <div class="enterprise-box" v-loading="loading" element-loading-text="正在进入企业中,请稍后...">
    <div class="e-title">请选择一个企业</div>
    <div class="e-box">
      <div class="e-item" v-for="(item, idx) in enterpriseList" :key="idx" @click="selectEnterprise(item.id)">
        <div class="e-inner-item">{{ item?.name ? item.name[0] : '' }}</div>
        <div class="e-inner-item-title">{{ item?.name }}</div>
      </div>
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import * as AcountTypes from '@/store/modules/Account/types'
import { getInitPath, getMenuList } from '@/utils/utils'

export default defineComponent({
  name: 'enterprise-com-list',
  components: {},
  data() {
    return {}
  },
  setup() {
    const router = useRouter()
    const enterpriseList = ref<Record<string, any>>([])
    const store = useStore()
    const loading = ref(false)
    enterpriseList.value = store.state.enterpriseList
    const selectEnterprise = async (id: number) => {
      loading.value = true
      // const { account: { openId } } = store.state as Record<string, any>
      store.commit(`${AcountTypes.MODULE_NAME}/${AcountTypes.SET_ACCOUNT_DATA}`, {
        enterpriseId: id,
      })
      const resources = await getMenuList(true, router)
      resources && router.replace(getInitPath(resources))
      loading.value = false
    }
    return {
      enterpriseList,
      loading,
      // methods
      selectEnterprise,
    }
  },
})
</script>

<style scoped lang="less">
.enterprise-box {
  width: 100%;
  min-height: 100vh;
  position: relative;
  .e-box {
    display: flex;
  }
  .e-title {
    padding: 20px;
    font-size: 14px;
    line-height: 1.5;
    color: rgba(0, 0, 0, 0.65);
    font-weight: bold;
  }
  .e-item {
    display: flex;
    flex-direction: column;
    border: 1px solid #efefef;
    margin: 10px 10px 10px 0;
    text-align: center;
    border-radius: 5px;
    float: left;
    cursor: pointer;
    &:hover {
      background: #f6fafe;
    }
    .e-inner-item {
      width: 100px;
      height: 100px;
      margin: 20px;
      line-height: 100px;
      background: #e0f2f1;
      font-size: 20px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .e-inner-item-title {
      font-size: 14px;
      padding-bottom: 10px;
    }
  }
}
</style>
